<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            function levelOrder(root) {
                if (!root) return []
                let queue = [root]
                let result = []
                while (queue.length) {
                    result.push([])
                    let len = queue.length
                    for (let i = 0; i < len; i++) {
                        let node = queue.shift()
                        result[result.length - 1].push(node.val)
                        let children = node.children || []
                        for (let i = 0; i < children.length; i++) {
                            queue.push(children[i])
                        }
                    }
                }
                return result
            }
        </script>
    </body>
</html>
